﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using Gree.Wuhu;

namespace Gree.Wuhu
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
            serverUrl = CommonUtils.getServerUrl();
            this.labelServerUrl.Text = serverUrl;
        }

        private const string pathInfo = "\\车牌识别图片\\";
        string serverUrl;

        string formTitle = "车牌识别-格力电器(芜湖)有限公司";

        static string[] strMemberType = new string[]{
            "临时车",
            "月卡",
            "贵宾卡",
            "储值卡",
            "领导车",
            "特殊车",
            "内部车",
            "白名单",
            "黑名单"
        };

        static string [] szPlateDefaultWord = new string[]{
	        "京",
	        "津",
	        "沪",
	        "渝",
	        "冀",
	        "晋",
	        "辽",
	        "吉",
	        "黑",
	        "苏",
	        "浙",
	        "皖",
	        "闽",
	        "赣",
	        "鲁",
	        "豫",
	        "鄂",
	        "湘",
	        "粤",
	        "琼",
	        "川",
	        "贵",
	        "云",
	        "陕",
	        "甘",
	        "宁",
	        "青",
	        "藏",
	        "桂",
	        "蒙",
	        "新",
	        "全国"
        };

        //去掉I，0两个英文字母
        static byte [] g_ucLocalCity = new byte[26 - 2 + 1];
        static uint [] g_uiPlateDefaultWord  = new uint [32];

        private static string strImageDir = "";
        private static int nCamId = -1;
        //private static MyClass.FGetImageCB fGetImageCB;
        private static MyClass.FGetImageCB2 fGetImageCB2;

        private static MyClass.FGetOffLinePayRecordCB fGetOffLinePayRecordCB;
        private static MyClass.FGetOffLineImageCBEx fGetOffLineImageCBEx;
        private static MyClass.FNetFindDeviceCallback fNetFindDeviceCallback;
        private static MyClass.FGetReportCBEx fGetReportCBEx;

        private static MyClass.T_VideoDetectParamSetup tVideoDetectParamSetup;
        private static MyClass.T_RS485Data tRS485Data;
        private static MyClass.T_DCTimeSetup tDCTimeSetup;
        private static MyClass.T_VehicleVAFunSetup tVehicleVAFunSetup;

        static int GetLocalCityIndex(byte ucLocalCity)
        {
            int nIndex = 0;
            for (int i = 0; i < g_ucLocalCity.Length; i++)
            {
                if (ucLocalCity == g_ucLocalCity[i])
                {
                    nIndex = i;
                    break;
                }
            }

            return nIndex;
        }

        public static uint Reverse_uint(uint uiNum)
        {
            return ((uiNum & 0x000000FF) << 24) |
                   ((uiNum & 0x0000FF00) << 8) |
                   ((uiNum & 0x00FF0000) >> 8) |
                   ((uiNum & 0xFF000000) >> 24);
        }

        //车牌识别结果上报回调，此方法是之前的方法，现在不使用了，升级了
        private int FGetImageCB(int tHandle, uint uiImageId, ref MyClass.T_ImageUserInfo tImageInfo, ref MyClass.T_PicInfo tPicInfo)
        {
            strImageDir = getImageDir();
            if (tHandle == nCamId)
            {
                string strTime = DateTime.Now.ToLocalTime().ToString("yyyyMMddHHmmssfff");
                //车辆图像
                if (tImageInfo.ucViolateCode == 0)
                {
                    string szLprResult = System.Text.Encoding.Default.GetString(tImageInfo.szLprResult).Replace("\0", "");
                    labelPlateNum.Text = szLprResult;

                    switch (tImageInfo.ucVehicleSize)//车型
                    {
                        case 0:
                            {
                                labelVehicSize.Text = "未知车型";
                                break;
                            }

                        case 1:
                            {
                                labelVehicSize.Text = "大型车";
                                break;
                            }
                        case 2:
                            {
                                labelVehicSize.Text = "中型车";
                                break;
                            }
                        case 3:
                            {
                                labelVehicSize.Text = "小型车";
                                break;
                            }
                        case 4:
                            {
                                labelVehicSize.Text = "摩托车";
                                break;
                            }
                        case 5:
                            {
                                labelVehicSize.Text = "行人";
                                break;
                            }
                        default:
                            {
                                labelVehicSize.Text = "未知车型";
                                break;
                            }
                    }

                    switch (tImageInfo.ucPlateColor)//车牌颜色
                    {
                        case 0:
                            labelPlateColor.Text = "蓝色";
                            labelPlateNum.BackColor = Color.Blue;
                            labelPlateNum.ForeColor = Color.White;
                            break;
                        case 1:
                            labelPlateColor.Text = "黄色";
                            labelPlateNum.BackColor = Color.Yellow;
                            labelPlateNum.ForeColor = Color.Black;
                            break;
                        case 2:
                            labelPlateColor.Text = "白色";
                            labelPlateNum.BackColor = Color.White;
                            labelPlateNum.ForeColor = Color.Black;
                            break;
                        case 3:
                            labelPlateColor.Text = "黑色";
                            labelPlateNum.BackColor = Color.Black;
                            labelPlateNum.ForeColor = Color.White;
                            break;
                        case 5:
                            labelPlateColor.Text = "绿色";
                            labelPlateNum.BackColor = Color.Green;
                            labelPlateNum.ForeColor = Color.White;
                            break;
                        case 6:
                            labelPlateColor.Text = "黄绿";
                            labelPlateNum.BackColor = Color.Green;
                            labelPlateNum.ForeColor = Color.White;
                            break;
                        case 4:
                        default:
                            labelPlateNum.Text = "未识别";
                            labelPlateNum.BackColor = Color.Blue;
                            break;
                    }

                    if (tPicInfo.ptPanoramaPicBuff != IntPtr.Zero && tPicInfo.uiPanoramaPicLen != 0)
                    {
                        byte[] BytePanoramaPicBuff = new byte[tPicInfo.uiPanoramaPicLen];
                        Marshal.Copy(tPicInfo.ptPanoramaPicBuff, BytePanoramaPicBuff, 0, (int)tPicInfo.uiPanoramaPicLen);
                        string strImageFile = String.Format("{0}\\{1}.jpg", strImageDir, strTime);
                        FileStream fs = new FileStream(strImageFile, FileMode.Create, FileAccess.Write | FileAccess.Read, FileShare.None);
                        fs.Write(BytePanoramaPicBuff, 0, (int)tPicInfo.uiPanoramaPicLen);
                        pictureBoxPlate.Image = Image.FromStream(fs);
                        fs.Close();
                        fs.Dispose();
                    }

                    if (tPicInfo.ptVehiclePicBuff != IntPtr.Zero && tPicInfo.uiVehiclePicLen != 0)
                    {
                        byte[] ByteVehiclePicBuff = new byte[tPicInfo.uiVehiclePicLen];
                        Marshal.Copy(tPicInfo.ptVehiclePicBuff, ByteVehiclePicBuff, 0, (int)tPicInfo.uiVehiclePicLen);
                        string strImageFile = String.Format("{0}\\{1}_plate.jpg", strImageDir, strTime);
                        FileStream fs = new FileStream(strImageFile, FileMode.Create, FileAccess.Write | FileAccess.Read, FileShare.None);
                        fs.Write(ByteVehiclePicBuff, 0, (int)tPicInfo.uiVehiclePicLen);
                        pictureBoxPlateImage.Image = Image.FromStream(fs);
                        fs.Close();
                        fs.Dispose();
                    }
                }
            }

            return 0;
        }

        private int FGetImageCB2(int tHandle, uint uiImageId, ref MyClass.T_ImageUserInfo2 tImageInfo, ref MyClass.T_PicInfo tPicInfo)
        {
            strImageDir = getImageDir();
            if (tHandle == nCamId)
            {
                string strTime = DateTime.Now.ToLocalTime().ToString("yyyyMMddHHmmssfff");
                //车辆图像
                if (tImageInfo.ucViolateCode == 0)
                {
                    string szLprResult = System.Text.Encoding.Default.GetString(tImageInfo.szLprResult).Replace("\0", "");
                    labelPlateNum.Text = szLprResult;

                    try
                    {
                        HttpPostHelper2.Post(this.serverUrl, "vehicleNo=" + szLprResult + "&clientIp=" + comboBoxIP.Text);
                        this.labelCameraInfo.Text = "车牌： " + szLprResult + " 上传成功！" + DateTime.Now.ToLocalTime().ToString("yyyy年MM月dd日 HH时mm分ss秒");
                    }
                    catch (Exception e)
                    {
                        
                        this.labelCameraInfo.Text = e.Message;
                    }

                    //车型
                    string strVehicleBrand = System.Text.Encoding.Default.GetString(tImageInfo.strVehicleBrand).Replace("\0", "");
                    labeVehicleBrand.Text = strVehicleBrand;

                    switch (tImageInfo.ucVehicleSize)//车辆类型
                    {
                        case 0:
                            {
                                labelVehicSize.Text = "未知车型";
                                break;
                            }

                        case 1:
                            {
                                labelVehicSize.Text = "大型车";
                                break;
                            }
                        case 2:
                            {
                                labelVehicSize.Text = "中型车";
                                break;
                            }
                        case 3:
                            {
                                labelVehicSize.Text = "小型车";
                                break;
                            }
                        case 4:
                            {
                                labelVehicSize.Text = "摩托车";
                                break;
                            }
                        case 5:
                            {
                                labelVehicSize.Text = "行人";
                                break;
                            }
                        default:
                            {
                                labelVehicSize.Text = "未知车型";
                                break;
                            }
                    }

                    switch (tImageInfo.ucPlateColor)//车牌颜色
                    {
                        case 0:
                            labelPlateColor.Text = "蓝色";
                            labelPlateNum.BackColor = Color.Blue;
                            labelPlateNum.ForeColor = Color.White;
                            break;
                        case 1:
                            labelPlateColor.Text = "黄色";
                            labelPlateNum.BackColor = Color.Yellow;
                            labelPlateNum.ForeColor = Color.Black;
                            break;
                        case 2:
                            labelPlateColor.Text = "白色";
                            labelPlateNum.BackColor = Color.White;
                            labelPlateNum.ForeColor = Color.Black;
                            break;
                        case 3:
                            labelPlateColor.Text = "黑色";
                            labelPlateNum.BackColor = Color.Black;
                            labelPlateNum.ForeColor = Color.White;
                            break;
                        case 5:
                            labelPlateColor.Text = "绿色";
                            labelPlateNum.BackColor = Color.Green;
                            labelPlateNum.ForeColor = Color.White;
                            break;
                        case 6:
                            labelPlateColor.Text = "黄绿";
                            labelPlateNum.BackColor = Color.Green;
                            labelPlateNum.ForeColor = Color.White;
                            break;
                        case 4:
                        default:
                            labelPlateNum.Text = "未识别";
                            labelPlateNum.BackColor = Color.Blue;
                            break;
                    }

                    if (tPicInfo.ptPanoramaPicBuff != IntPtr.Zero && tPicInfo.uiPanoramaPicLen != 0)
                    {
                        byte[] BytePanoramaPicBuff = new byte[tPicInfo.uiPanoramaPicLen];
                        Marshal.Copy(tPicInfo.ptPanoramaPicBuff, BytePanoramaPicBuff, 0, (int)tPicInfo.uiPanoramaPicLen);
                        string strImageFile = String.Format("{0}\\{1}.jpg", strImageDir, strTime);
                        FileStream fs = new FileStream(strImageFile, FileMode.Create, FileAccess.Write | FileAccess.Read, FileShare.None);
                        fs.Write(BytePanoramaPicBuff, 0, (int)tPicInfo.uiPanoramaPicLen);
                        pictureBoxPlate.Image = Image.FromStream(fs);
                        fs.Close();
                        fs.Dispose();
                    }

                    if (tPicInfo.ptVehiclePicBuff != IntPtr.Zero && tPicInfo.uiVehiclePicLen != 0)
                    {
                        byte[] ByteVehiclePicBuff = new byte[tPicInfo.uiVehiclePicLen];
                        Marshal.Copy(tPicInfo.ptVehiclePicBuff, ByteVehiclePicBuff, 0, (int)tPicInfo.uiVehiclePicLen);
                        string strImageFile = String.Format("{0}\\{1}_plate.jpg", strImageDir, strTime);
                        FileStream fs = new FileStream(strImageFile, FileMode.Create, FileAccess.Write | FileAccess.Read, FileShare.None);
                        fs.Write(ByteVehiclePicBuff, 0, (int)tPicInfo.uiVehiclePicLen);
                        pictureBoxPlateImage.Image = Image.FromStream(fs);
                        fs.Close();
                        fs.Dispose();
                    }
                }
            }

            return 0;
        }

        /// <summary>
        /// 根据日期来生成对应的目录
        /// </summary>
        /// <returns>返回目录</returns>
        private string getImageDir()
        {
            //strImageDir = System.Windows.Forms.Application.StartupPath + "\\image\\" + DateTime.Now.ToLocalTime().ToString("yyyyMMdd");
            var startupPath = System.Windows.Forms.Application.StartupPath;
            strImageDir = startupPath.Substring(0, 2) + pathInfo + DateTime.Now.ToLocalTime().ToString("yyyyMMdd");
            if (!Directory.Exists(strImageDir))
            {
                Directory.CreateDirectory(strImageDir);
            }
            return strImageDir;
        }

        //脱机收费数据回调
        private int FGetOffLinePayRecordCB(int tHandle, byte ucType, ref MyClass.T_VehPayRsp ptVehPayInfo, uint uiLen, ref MyClass.T_PicInfo ptPicInfo, IntPtr obj)
        {
            if (tHandle == nCamId)
            {
                ListViewItem lvi = new ListViewItem(System.Text.Encoding.Default.GetString(ptVehPayInfo.acPlate), 0);
                lvi.SubItems.Add(System.Text.Encoding.Default.GetString(ptVehPayInfo.acEntryTime));
                lvi.SubItems.Add(System.Text.Encoding.Default.GetString(ptVehPayInfo.acExitTime));
                lvi.SubItems.Add(String.Format("{0}元", ptVehPayInfo.uiRequired / 10));
                lvi.SubItems.Add(String.Format("{0}元", ptVehPayInfo.uiPrepaid / 10));
                lvi.SubItems.Add(ptVehPayInfo.ucVehType == 1 ? "小车" : "大车");
                lvi.SubItems.Add(strMemberType[ptVehPayInfo.ucUserType]);
                lvi.SubItems.Add(ptVehPayInfo.ucResultCode == 1 ? "成功" : "无记录");
            }
           
            return 0;
        }

        //脱机数据回调
        private int FGetOffLineImageCBEx(int tHandle, uint uiImageId, ref MyClass.T_ImageUserInfo tImageInfo, ref MyClass.T_PicInfo tPicInfo, IntPtr obj)
        {
            if (tHandle == nCamId)
            {
                //string strTime = DateTime.Now.ToLocalTime().ToString("yyyyMMddHHmmssfff");
                ////车辆图像
                //if (tImageInfo.ucViolateCode == 0)
                //{
                //    string szSnapTime = System.Text.Encoding.Default.GetString(tImageInfo.acSnapTime);
                //    string szLprResult = System.Text.Encoding.Default.GetString(tImageInfo.szLprResult).Replace("\0", "");
                //    labelPlateNum.Text = szLprResult;

                //    switch (tImageInfo.ucVehicleSize)//车型
                //    {
                //        case 0:
                //            {
                //                labelVehicSize.Text = "未知车型";
                //                break;
                //            }

                //        case 1:
                //            {
                //                labelVehicSize.Text = "大型车";
                //                break;
                //            }
                //        case 2:
                //            {
                //                labelVehicSize.Text = "中型车";
                //                break;
                //            }
                //        case 3:
                //            {
                //                labelVehicSize.Text = "小型车";
                //                break;
                //            }
                //        case 4:
                //            {
                //                labelVehicSize.Text = "摩托车";
                //                break;
                //            }
                //        case 5:
                //            {
                //                labelVehicSize.Text = "行人";
                //                break;
                //            }
                //        default:
                //            {
                //                labelVehicSize.Text = "未知车型";
                //                break;
                //            }
                //    }

                //    switch (tImageInfo.ucPlateColor)//车牌颜色
                //    {
                //        case 0:
                //            labelPlateColor.Text = "蓝色";
                //            labelPlateNum.BackColor = Color.Blue;
                //            labelPlateNum.ForeColor = Color.White;
                //            break;
                //        case 1:
                //            labelPlateColor.Text = "黄色";
                //            labelPlateNum.BackColor = Color.Yellow;
                //            labelPlateNum.ForeColor = Color.Black;
                //            break;
                //        case 2:
                //            labelPlateColor.Text = "白色";
                //            labelPlateNum.BackColor = Color.White;
                //            labelPlateNum.ForeColor = Color.Black;
                //            break;
                //        case 3:
                //            labelPlateColor.Text = "黑色";
                //            labelPlateNum.BackColor = Color.Black;
                //            labelPlateNum.ForeColor = Color.White;
                //            break;
                //        case 4:
                //        default:
                //            labelPlateNum.Text = "未识别";
                //            labelPlateNum.BackColor = Color.Blue;
                //            break;
                //    }

                //    if (tPicInfo.ptPanoramaPicBuff != IntPtr.Zero && tPicInfo.uiPanoramaPicLen != 0)
                //    {
                //        byte[] BytePanoramaPicBuff = new byte[tPicInfo.uiPanoramaPicLen];
                //        Marshal.Copy(tPicInfo.ptPanoramaPicBuff, BytePanoramaPicBuff, 0, (int)tPicInfo.uiPanoramaPicLen);
                //        string strImageFile = String.Format("{0}\\{1}.jpg", strImageDir, strTime);
                //        FileStream fs = new FileStream(strImageFile, FileMode.Create, FileAccess.Write | FileAccess.Read, FileShare.None);
                //        fs.Write(BytePanoramaPicBuff, 0, (int)tPicInfo.uiPanoramaPicLen);
                //        pictureBoxPlate.Image = Image.FromStream(fs);
                //        fs.Close();
                //        fs.Dispose();
                //    }

                //    if (tPicInfo.ptVehiclePicBuff != IntPtr.Zero && tPicInfo.uiVehiclePicLen != 0)
                //    {
                //        byte[] ByteVehiclePicBuff = new byte[tPicInfo.uiVehiclePicLen];
                //        Marshal.Copy(tPicInfo.ptVehiclePicBuff, ByteVehiclePicBuff, 0, (int)tPicInfo.uiVehiclePicLen);
                //        string strImageFile = String.Format("{0}\\{1}_plate.jpg", strImageDir, strTime);
                //        FileStream fs = new FileStream(strImageFile, FileMode.Create, FileAccess.Write | FileAccess.Read, FileShare.None);
                //        fs.Write(ByteVehiclePicBuff, 0, (int)tPicInfo.uiVehiclePicLen);
                //        pictureBoxPlateImage.Image = Image.FromStream(fs);
                //        fs.Close();
                //        fs.Dispose();
                //    }
                //}
            }
           
            return 0;
        }

        private int FNetFindDeviceCallback(ref MyClass.T_RcvMsg ptFindDevice, IntPtr obj)
        {
            return 0;
        }

        private int FGetReportCBEx(int tHandle, byte ucType, IntPtr ptMessage, IntPtr pUserData)
        {
            string strMsg = String.Format("{0}", ucType);

            //MessageBox.Show(strMsg);

            if (14 == ucType) //语音对讲通知
            {
                if (DialogResult.Yes == MessageBox.Show("是否接受相机端发起的语音对讲", "提示", MessageBoxButtons.YesNo))
                {
                    buttonTalk_Click(null, null);
                }
            }

            return 0;
        }

        private void Demo_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;

            MyClass.Net_Init();

            int ptLen = 255;
            StringBuilder strVersion = new StringBuilder(ptLen);
            MyClass.Net_GetSdkVersion(strVersion, ref ptLen);
            this.Text += "(" + strVersion + ")";

            fNetFindDeviceCallback = new MyClass.FNetFindDeviceCallback(FNetFindDeviceCallback);

            strImageDir = getImageDir();//初始化路径信息

            //dateTimePickerCAM.Format = DateTimePickerFormat.Custom;
            //dateTimePickerCAM.CustomFormat = "yyyy-MM-dd HH:mm:ss";

            for (int i = 0; i < szPlateDefaultWord.Length; i++ )
            {
                if (i != szPlateDefaultWord.Length - 1)
                {
                     byte[] utf8 = System.Text.Encoding.UTF8.GetBytes(szPlateDefaultWord[i]);
                     g_uiPlateDefaultWord[i] = (uint)((utf8[2] << 16) | (utf8[1] << 8) | utf8[0]);
                }
                else
                {
                    g_uiPlateDefaultWord[i] = 0;
                }
            }

            int nLocalCityIndex = 0;
            for (int i = 'A'; i <= 'Z'; i++)
            {
                if (i == 'I' || i == 'O')
                {
                    continue;
                }
                g_ucLocalCity[nLocalCityIndex++] = (byte)i;
            }
            g_ucLocalCity[nLocalCityIndex++] = 0;
        }

        private void Demo_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (nCamId != -1)
            {
                MyClass.Net_StopVideo(nCamId);
                MyClass.Net_DisConnCamera(nCamId);
                MyClass.Net_DelCamera(nCamId);
                nCamId = -1;
            }
            MyClass.Net_UNinit();
        }

        private void button_Connect_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            do 
            {
                if (comboBoxIP.Text == "")
                {
                    MessageBox.Show("请输入相机IP!", "提示");
                    break;
                }

                nCamId = MyClass.Net_AddCamera(comboBoxIP.Text);
                if (nCamId == -1)
                {
                    MessageBox.Show("添加相机失败!", "提示");
                    break;
                }

                int iRet = MyClass.Net_ConnCamera(nCamId, 30000, 10);
                if (iRet != 0)
                {
                    MyClass.Net_DelCamera(nCamId);
                    MessageBox.Show("连接相机失败!", "提示");
                    break;
                }

                iRet = MyClass.Net_StartVideo(nCamId, 0, tabPageVideo.Handle);
                if (iRet != 0)
                {
                    MyClass.Net_DisConnCamera(nCamId);
                    MyClass.Net_DelCamera(nCamId);
                    MessageBox.Show("打开视频失败!", "提示");
                    break;
                }

                MyClass.Net_RegOffLineClient(nCamId);

                this.labelCameraInfo.Text = "相机连接成功！可以识别车牌！";
                this.labelCameraInfo.ForeColor = Color.Green;

                //fGetImageCB = new MyClass.FGetImageCB(FGetImageCB);
                //MyClass.Net_RegImageRecv(fGetImageCB);
                fGetImageCB2 = new MyClass.FGetImageCB2(FGetImageCB2);
                MyClass.Net_RegImageRecv2(fGetImageCB2);

                fGetOffLinePayRecordCB = new MyClass.FGetOffLinePayRecordCB(FGetOffLinePayRecordCB);
                MyClass.Net_RegOffLinePayRecord(nCamId, fGetOffLinePayRecordCB, IntPtr.Zero);

                fGetOffLineImageCBEx = new MyClass.FGetOffLineImageCBEx(FGetOffLineImageCBEx);
                MyClass.Net_RegOffLineImageRecvEx(nCamId, fGetOffLineImageCBEx, IntPtr.Zero);

                fGetReportCBEx = new MyClass.FGetReportCBEx(FGetReportCBEx);
                MyClass.Net_RegReportMessEx(nCamId, fGetReportCBEx, IntPtr.Zero);

                iRet = MyClass.Net_QueryVideoDetectSetup(nCamId, ref tVideoDetectParamSetup);
                if (iRet != 0)
                {
                    //MessageBox.Show("查询视频检测区域参数失败!", "提示");
                }
                else
                {
                }

                buttonVehicleVAFunQuery_Click(sender, e);

            } while (false);

            Cursor.Current = Cursors.Default;
        }

        private void button_DisConnect_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            if (nCamId != -1)
            {
                MyClass.Net_StopVideo(nCamId);
                MyClass.Net_DisConnCamera(nCamId);
                MyClass.Net_DelCamera(nCamId);
                nCamId = -1;
            }
            Cursor.Current = Cursors.Default;
            this.labelCameraInfo.Text = "相机断开成功！请重新连接...";
            this.labelCameraInfo.ForeColor = Color.Red;
        }

        private void buttonGrab_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            MyClass.T_FrameInfo tFrameInfo = new MyClass.T_FrameInfo();

            int iRet = MyClass.Net_ImageSnap(nCamId, ref tFrameInfo);
            if (iRet == 0)
            {
                //MessageBox.Show("抓拍成功", "提示");
            }
            else
            {
                MessageBox.Show("抓拍失败", "提示");
            }
        }

        private void buttonGate_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            MyClass.T_ControlGate tControlGate = new MyClass.T_ControlGate();
            tControlGate.ucState = 1;
            tControlGate.ucReserved = new byte[3]{ 0,0,0};

            int iRet = MyClass.Net_GateSetup(nCamId, ref tControlGate);
            if (iRet == 0)
            {
                MessageBox.Show("抬闸成功", "提示");
            }
            else
            {
                MessageBox.Show("抬闸失败", "提示");
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            MyClass.T_LprResult tLprResult = new MyClass.T_LprResult();
            tLprResult.LprResult = new byte[16];

            tLprResult.StartTime = new byte[16];

            tLprResult.EndTime = new byte[16];

            MyClass.T_BlackWhiteListCount tBlackWhiteListCount = new MyClass.T_BlackWhiteListCount();
            tBlackWhiteListCount.uiCount = 1;
            string strAucLplPath = System.Windows.Forms.Application.StartupPath + "\\lpr.ini";
            if (File.Exists(strAucLplPath))
            {
                File.Delete(strAucLplPath);
            }
            byte[] aucLplPath = System.Text.Encoding.Default.GetBytes(strAucLplPath);
            tBlackWhiteListCount.aucLplPath = new byte[256];
            aucLplPath.CopyTo(tBlackWhiteListCount.aucLplPath, 0);

            int iRet = MyClass.Net_BlackWhiteListSetup(ref tLprResult, ref tBlackWhiteListCount);
            if (iRet == 0)
            {

            }
            else
            {
                MessageBox.Show("生成白名单失败", "提示");
            }

            MyClass.T_BlackWhiteList tBalckWhiteList = new MyClass.T_BlackWhiteList();
            tBalckWhiteList.LprMode = 1;
            tBalckWhiteList.Lprnew = 0;
            tBalckWhiteList.aucLplPath = new byte[256];
            aucLplPath.CopyTo(tBalckWhiteList.aucLplPath, 0);

            iRet = MyClass.Net_BlackWhiteListSend(nCamId, ref tBalckWhiteList);
            if (iRet == 0)
            {
                MessageBox.Show("重设白名单成功", "提示");
            }
            else
            {
                MessageBox.Show("重设白名单失败", "提示");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            MyClass.T_LprResult tLprResult = new MyClass.T_LprResult();
            tLprResult.LprResult = new byte[16];

            tLprResult.StartTime = new byte[16];

            tLprResult.EndTime = new byte[16];

            MyClass.T_BlackWhiteListCount tBlackWhiteListCount = new MyClass.T_BlackWhiteListCount();
            tBlackWhiteListCount.uiCount = 1;
            string strAucLplPath = System.Windows.Forms.Application.StartupPath + "\\lpr.ini";
            if (File.Exists(strAucLplPath))
            {
                File.Delete(strAucLplPath);
            }
            byte[] aucLplPath = System.Text.Encoding.Default.GetBytes(strAucLplPath);
            tBlackWhiteListCount.aucLplPath = new byte[256];
            aucLplPath.CopyTo(tBlackWhiteListCount.aucLplPath, 0);

            int iRet = MyClass.Net_BlackWhiteListSetup(ref tLprResult, ref tBlackWhiteListCount);
            if (iRet == 0)
            {

            }
            else
            {
                MessageBox.Show("生成白名单失败", "提示");
            }

            MyClass.T_BlackWhiteList tBalckWhiteList = new MyClass.T_BlackWhiteList();
            tBalckWhiteList.LprMode = 1;
            tBalckWhiteList.Lprnew = 1;
            tBalckWhiteList.aucLplPath = new byte[256];
            aucLplPath.CopyTo(tBalckWhiteList.aucLplPath, 0);

            iRet = MyClass.Net_BlackWhiteListSend(nCamId, ref tBalckWhiteList);
            if (iRet == 0)
            {
                MessageBox.Show("新增白名单成功", "提示");
            }
            else
            {
                MessageBox.Show("新增白名单失败", "提示");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            MyClass.T_LprResult tLprResult = new MyClass.T_LprResult();
            tLprResult.LprResult = new byte[16];

            tLprResult.StartTime = new byte[16];

            tLprResult.EndTime = new byte[16];

            MyClass.T_BlackWhiteListCount tBlackWhiteListCount = new MyClass.T_BlackWhiteListCount();
            tBlackWhiteListCount.uiCount = 1;
            string strAucLplPath = System.Windows.Forms.Application.StartupPath + "\\lpr.ini";
            if (File.Exists(strAucLplPath))
            {
                File.Delete(strAucLplPath);
            }
            byte[] aucLplPath = System.Text.Encoding.Default.GetBytes(strAucLplPath);
            tBlackWhiteListCount.aucLplPath = new byte[256];
            aucLplPath.CopyTo(tBlackWhiteListCount.aucLplPath, 0);

            int iRet = MyClass.Net_BlackWhiteListSetup(ref tLprResult, ref tBlackWhiteListCount);
            if (iRet == 0)
            {

            }
            else
            {
                MessageBox.Show("生成白名单失败", "提示");
                return;
            }

            MyClass.T_BlackWhiteList tBalckWhiteList = new MyClass.T_BlackWhiteList();
            tBalckWhiteList.LprMode = 1;
            tBalckWhiteList.Lprnew = 2;
            tBalckWhiteList.aucLplPath = new byte[256];
            aucLplPath.CopyTo(tBalckWhiteList.aucLplPath, 0);

            iRet = MyClass.Net_BlackWhiteListSend(nCamId, ref tBalckWhiteList);
            if (iRet == 0)
            {
                MessageBox.Show("删除白名单成功", "提示");
            }
            else
            {
                MessageBox.Show("删除白名单失败", "提示");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            MyClass.T_LprResult tLprResult = new MyClass.T_LprResult();
            tLprResult.LprResult = new byte[16];

            tLprResult.StartTime = new byte[16];

            tLprResult.EndTime = new byte[16];

            MyClass.T_BlackWhiteListCount tBlackWhiteListCount = new MyClass.T_BlackWhiteListCount();
            tBlackWhiteListCount.uiCount = 1;
            string strAucLplPath = System.Windows.Forms.Application.StartupPath + "\\lpr.ini";
            if (File.Exists(strAucLplPath))
            {
                File.Delete(strAucLplPath);
            }

            byte[] aucLplPath = System.Text.Encoding.Default.GetBytes(strAucLplPath);
            tBlackWhiteListCount.aucLplPath = new byte[256];
            aucLplPath.CopyTo(tBlackWhiteListCount.aucLplPath, 0);

            int iRet = MyClass.Net_BlackWhiteListSetup(ref tLprResult, ref tBlackWhiteListCount);
            if (iRet == 0)
            {
                //MessageBox.Show("生成白名单成功", "提示");
                Process.Start("Notepad.exe", strAucLplPath);
            }
            else
            {
                MessageBox.Show("生成白名单失败", "提示");
            }
        }

        private void buttonAutoGrab_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            //////////////////////////////////////////////////////////////////////////////
            //直接保存为JPG图片
            //string strTime = DateTime.Now.ToLocalTime().ToString("yyyyMMddHHmmssfff");
            //string strJpgPath = strTime + ".jpg";
            //int iRet = MyClass.Net_SaveJpgFile(nCamId, strJpgPath);
            //if (iRet == 0)
            //{
            //    //MessageBox.Show("手动抓图成功", "提示");
            //    if (File.Exists(strJpgPath))
            //    {
            //         Process.Start("Explorer.exe", strJpgPath);
            //    }
            //}
            //else
            //{
            //    MessageBox.Show("手动抓图失败", "提示");
            //}

            ////////////////////////////////////////////////////////////////////////
            //获取当前帧的JPG缓存
            IntPtr ucJpgBuffer = IntPtr.Zero; 
            ulong ulSize = 0;

            int iRet = MyClass.Net_GetJpgBuffer(nCamId, ref ucJpgBuffer, ref ulSize);

            if (iRet == 0)
            {
                //将当前帧的JPG缓存刷到控件上
                byte[] ByteJpgBuffer = new byte[ulSize];
                Marshal.Copy(ucJpgBuffer, ByteJpgBuffer, 0, (int)ulSize);
                MemoryStream ms = new MemoryStream(ByteJpgBuffer);
                pictureBoxPlate.Image = Image.FromStream(ms);


                //////////////////////////////////////////////////////////////////////
                //将缓存保存为JPG图片
                //string strTime = DateTime.Now.ToLocalTime().ToString("yyyyMMddHHmmssfff");
                //string strJpgPath = strTime + ".jpg";
                //FileStream fs = new FileStream(strJpgPath, FileMode.Create, FileAccess.Write | FileAccess.Read, FileShare.None);
                //fs.Write(ByteJpgBuffer, 0, (int)ulSize);
                //fs.Close();
                //fs.Dispose();

                //if (File.Exists(strJpgPath))
                //{
                //    Process.Start("Explorer.exe", strJpgPath);
                //}
            }
            else
            {
                MessageBox.Show("手动抓图失败", "提示");
            }

            if (ucJpgBuffer != IntPtr.Zero)
            {
                MyClass.Net_FreeBuffer(ucJpgBuffer);
                ucJpgBuffer = IntPtr.Zero;
            }

        }
        private void buttonEncpyptionRead_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            int ptLen = 256; //长度需要大于256
            StringBuilder strEncpyption = new StringBuilder(ptLen);
            int iRet = MyClass.Net_ReadTwoEncpyption(nCamId, strEncpyption, (uint)ptLen);
            if (iRet == -1)
            {
                MessageBox.Show("读取加密数据失败", "提示");
                return;
            }

        }

        private void buttonEncpyptionWrite_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            //统计Ansi字节数

        }

        private void buttonPlateSet_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }


            int iRet = MyClass.Net_VideoDetectSetup(nCamId, ref tVideoDetectParamSetup);
            if (iRet != 0)
            {
                MessageBox.Show("设置视频检测区域参数失败!", "提示");
            }

            MyClass.Net_UpdatePlateRegion(nCamId);
        }

        private void buttonRs485Send_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }


            tRS485Data.rs485Id = 0;
            tRS485Data.dataLen = 0;
            tRS485Data.data = new byte [1024];


            int iRet = MyClass.Net_SendRS485Data(nCamId, ref tRS485Data);
            if (iRet != 0)
            {
                MessageBox.Show("发送Rs485数据失败!", "提示");
            }
            else
            {
                MessageBox.Show("发送Rs485数据成功!", "提示");
            }
        }

        private void checkBoxShowPlateRegion_CheckedChanged(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }
        }

        private void buttonTimeRead_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            int iRet = MyClass.Net_QueryTimeSetup(nCamId, ref tDCTimeSetup);
            if (iRet != 0)
            {
                MessageBox.Show("查询相机时间失败!", "提示");
                return;
            }

            dateTimePickerCAM.Value = new DateTime(tDCTimeSetup.usYear, tDCTimeSetup.ucMonth, tDCTimeSetup.ucDay,
                                                   tDCTimeSetup.ucHour, tDCTimeSetup.ucMinute, tDCTimeSetup.ucSecond);
        }

        private void buttonTimeWrite_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            tDCTimeSetup.usYear = (ushort)dateTimePickerCAM.Value.Year;
            tDCTimeSetup.ucMonth = (byte)dateTimePickerCAM.Value.Month;
            tDCTimeSetup.ucDay = (byte)dateTimePickerCAM.Value.Day;

            tDCTimeSetup.ucHour = (byte)dateTimePickerCAM.Value.Hour;
            tDCTimeSetup.ucMinute = (byte)dateTimePickerCAM.Value.Minute;
            tDCTimeSetup.ucSecond = (byte)dateTimePickerCAM.Value.Second;

            int iRet = MyClass.Net_TimeSetup(nCamId, ref tDCTimeSetup);
            if (iRet != 0)
            {
                MessageBox.Show("设置相机时间失败!", "提示");
                return;
            }
        }

        private void buttonVehicleVAFunSetup_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            int iRet = MyClass.Net_VehicleVAFunSetup(nCamId, ref tVehicleVAFunSetup);
            if (iRet != 0)
            {
                MessageBox.Show("设置相机默认字失败!", "提示");
                return;
            }
        }

        private void buttonVehicleVAFunQuery_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            int iRet = MyClass.Net_QueryVehicleVAFunSetup(nCamId, ref tVehicleVAFunSetup);
            if (iRet != 0)
            {
                MessageBox.Show("查询相机默认字失败!", "提示");
                return;
            }

            for (int i = 0; i < g_uiPlateDefaultWord.Length; i++)
		    {
			    if (g_uiPlateDefaultWord[i] == tVehicleVAFunSetup.uiPlateDefaultWord)
			    {
				    break;
			    }
		    }

            byte ucLocalCity = g_ucLocalCity[GetLocalCityIndex(tVehicleVAFunSetup.ucLocalCity)];
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            this.labelCameraInfo.Text = "正在搜索中，请等待...";
            this.labelCameraInfo.ForeColor = Color.Blue;
            Cursor.Current = Cursors.WaitCursor;
            comboBoxIP.Items.Clear();
            comboBoxIP.Sorted = true;
            MyClass.Net_FindDevice(fNetFindDeviceCallback, IntPtr.Zero);
            comboBoxIP.DroppedDown = true;
            Cursor.Current = Cursors.Default;
            this.labelCameraInfo.Text = "搜索完毕！请选择相机！";
        }

        private static bool m_bTalking = false;
        private void buttonTalk_Click(object sender, EventArgs e)
        {
            if (nCamId == -1)
            {
                MessageBox.Show("相机未连接", "提示");
                return;
            }

            int iRet = 0;

            if (false == m_bTalking)
            {
                iRet = MyClass.Net_StartTalk(nCamId);

                if (0 == iRet)
                {
                    m_bTalking = true;
                    MessageBox.Show("开始对讲成功", "提示");
                }
                else
                {
                    MessageBox.Show("开始对讲失败", "提示");
                }
            }
            else
            {
                iRet = MyClass.Net_StopTalk(nCamId);

                if (0 == iRet)
                {
                    m_bTalking = false;
                    MessageBox.Show("结束对讲成功", "提示");
                }
                else
                {
                    MessageBox.Show("结束对讲失败", "提示");
                   
                }
            }
        }

        private void comboBoxIP_SelectedValueChanged(object sender, EventArgs e)
        {
            string selectedText = this.comboBoxIP.SelectedItem.ToString();
            this.Text = selectedText + " " + this.formTitle;
        }
    }
}